package com.prj.ufdm.auth.bpo._impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.prj.ufdm.auth.bpo.SysUserBPO;
import com.prj.ufdm.auth.dao.SysUserDao;
import com.prj.ufdm.auth.dao.SysUserRoleDao;
import com.prj.ufdm.auth.model.SysUser;
import com.prj.ufdm.auth.model.SysUserRole;
import com.prj.ufdm.core.bpo._impl.BaseBPOImpl;
import com.prj.ufdm.core.util.UfdmUUIDUtil;

/**
 * 系统用户管理
 */
@Service 
public class SysUserBPOImpl extends BaseBPOImpl<SysUserDao,SysUser> implements SysUserBPO {
	
	@Autowired
	SysUserDao sysUserDao;
	
	@Autowired
	SysUserRoleDao sysUserRoleDao;
	
	/** 
	 * 描述: 通过用户名查询用户信息
	 * @auther 胡义振
	 * @date 2013-8-16 
	 * @param sysUser
	 * @return 
	 */
	public SysUser doGetSysUserByUsername(String username) throws Exception
	{
		return sysUserDao.selectSysUserByUsername(username);
	}
	
	/** 
	 * 描述: 通过TOKEN查询用户信息
	 * @auther 胡义振
	 * @date 2013-8-16 
	 * @param token 
	 * @return 
	 */
	public SysUser doGetSysUserByToken(String token) throws Exception
	{
		return sysUserDao.selectSysUserByToken(token);
	}
	
	
	/** 
	 * 描述: 根据用户名、密码获取用户信息
	 * @auther 胡义振
	 * @date 2014-1-9 
	 * @param 
	 *     username 
	 *     password
	 * @return 
	 */
	public SysUser doGetSysUserByUserNameAndPassword(String username,String password) throws Exception
	{
		SysUser sysUser  = new SysUser();
		sysUser.setUsername(username);
		sysUser.setPassword(password);
		return sysUserDao.selectSysUserForLogin(sysUser);
	}
	
	
	/** 
	 * 描述: 检测登录名是否存在（userId 为空，检测新增用户名是否存在。）
	 * @auther 胡义振
	 * @date 2014-1-9 
	 * @param 
	 *     username 用户名
	 *     userId   用户ID
	 * @return 
	 */
	public boolean doCheckSysUserLoginNameExist(String username,String userId) throws Exception
	{
		SysUser sysUser = new SysUser();
		sysUser.setUsername(username);
		//检测新增用户的登录名
		if(userId!=null && !userId.equals("")){
			sysUser.setId(userId);
		}
		Integer searchCount = sysUserDao.selectSysUserForCheckUsername(sysUser);
		if(searchCount!=null && searchCount>0){
			return true;
		}
		return false;
	}

 	/**
 	 * 描述: 通过角色ID查询该角色下的用户
 	 * @auther 胡义振
 	 * @date 2018-03-22
 	 * @param sysUser  角色用户
 	 * @return
 	 */
	@Override
	public List<SysUser> doGetSysUserListWithRoleId(SysUser sysUser) throws Exception {
		return sysUserDao.selectSysUserListWithRoleId(sysUser);
	}

	/**
	 * 根据用户ID删除系统用户
	 * @param id 要删除的角色ID（多个ID，逗号分隔）
	 * @return
	 * @throws Exception
	 * @author 胡义振  
	 * @date 2018年3月27日
	 */
	@Override
	public Integer doRmSysUserById(String userIds) throws Exception {
		if (userIds == null)  userIds = "";
		String[] arrUserId = userIds.split(",");
		for(String userId : arrUserId){
			// 删除用户角色对应关系
			sysUserRoleDao.deleteUserRoleByUserId(userId);
			// 删除用户表
			sysUserDao.deleteById(userId);
		}
		return 0;
	}
	
	/**
	 * 在用户中添加角色
	 * @param 
	 *      userId      用户ID
	 *      roleIds     角色ID（多个用","号分隔）
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	public Integer doAddSysUserRole(String userId,String roleIds) throws Exception {
		if (roleIds == null)  roleIds = "";
		sysUserRoleDao.deleteUserRoleByUserId(userId);
		String[] arrRoleId = roleIds.split(",");
		for(String roleId : arrRoleId){
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setId(UfdmUUIDUtil.getUUID());
			sysUserRole.setRoleId(roleId);
			sysUserRole.setUserId(userId);
			sysUserRoleDao.insertOne(sysUserRole);
		}
		return arrRoleId.length;
	}
	
	/**
	 * 删除用户中的角色
	 * @param 
	 *      userId      用户ID
	 *      roleIds     角色ID（多个用","号分隔）
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	public Integer doRmSysUserRole(String userId,String roleIds) throws Exception{
		if (roleIds == null)  roleIds = "";
		String[] arrRoleId = roleIds.split(",");
		for(String roleId : arrRoleId){
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setId(UfdmUUIDUtil.getUUID());
			sysUserRole.setRoleId(roleId);
			sysUserRole.setUserId(userId);
			sysUserRoleDao.deleteUserRoleByUserIdAndRoleId(sysUserRole);
		}
		return arrRoleId.length;
	}

}
